//
//  FLLSelectedMoreViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/12.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLSelectedMoreViewController.h"
#import "ZWYNetTool.h"
#import "FLLMacro.h"
#import "Masonry.h"
#import "FLLNewestModel.h"
#import "SelectedMoreModel.h"
#import "SelectedMoreData.h"
#import "SelectedMoreComment.h"
#import "SelectedMoreSecData.h"
#import "SelectedMoreTopics.h"
#import "SelectedMoreParents.h"
#import "MJRefresh.h"
#import "FLLNewestCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "FLLWorksDetailViewController.h"

@interface FLLSelectedMoreViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *selectedMoreCollectionView;
@property (nonatomic, strong) SelectedMoreModel *selectedMoreModel;
@property (nonatomic, strong) NSMutableArray<SelectedMoreSecData *> *secDataArray;
@property (nonatomic, assign) NSInteger pageCount;
@property (nonatomic, assign) BOOL isRefresh;
//@property (nonatomic, strong) NSString *post_ID;
//@property (nonatomic, strong) NSMutableArray<SelectedMoreTopics *> *post_IDArray;
@end

@implementation FLLSelectedMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createPOSTRequest];
    [self createSelectedMoreCollectionView];
    
}
- (void)createPOSTRequest
{
    
    NSString *bodyString = [NSString stringWithFormat:@"is_marrow=1&page=%zd&methodName=ShequList&size=20&version=4.40&", _pageCount];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        
        // 加载
        if (_isRefresh == YES) {
            _pageCount++;
            
        }
        // 起始或者刷新
        else
        {
            [self.secDataArray removeAllObjects];
            self.pageCount = 1;
        }

 
        self.selectedMoreModel = [SelectedMoreModel modelWithDic:result];
        // 加载
        if (_isRefresh == YES) {
            [self.secDataArray addObjectsFromArray:(NSMutableArray *)self.selectedMoreModel.data.data];

            // 起始或者刷新
        } else {
            self.secDataArray = (NSMutableArray *)self.selectedMoreModel.data.data;
        }
        [self.selectedMoreCollectionView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)createSelectedMoreCollectionView
{
    
    UICollectionViewFlowLayout *selectedMoreLayout = [[UICollectionViewFlowLayout alloc] init];
    selectedMoreLayout.minimumInteritemSpacing = CellMargin;
    selectedMoreLayout.minimumLineSpacing = 0;
    selectedMoreLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.selectedMoreCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:selectedMoreLayout];
    self.selectedMoreCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_selectedMoreCollectionView];
    [self.selectedMoreCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
    self.selectedMoreCollectionView.delegate = self;
    self.selectedMoreCollectionView.dataSource = self;
    [self.selectedMoreCollectionView registerNib:[UINib nibWithNibName:@"FLLNewestCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"newestCollectionCell"];
#pragma mark -- 上拉加载 下拉刷新
    self.selectedMoreCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isRefresh = NO;
        [self createPOSTRequest];
        [self.selectedMoreCollectionView.mj_header endRefreshing];
    }];
    
    self.selectedMoreCollectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _isRefresh = YES;
        [self createPOSTRequest];
        [self.selectedMoreCollectionView.mj_footer endRefreshing];
    }];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _secDataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FLLNewestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newestCollectionCell" forIndexPath:indexPath];
    
    cell.grayLabel.backgroundColor = FLLLightGrayColor;
    cell.whiteLabel.backgroundColor = [UIColor whiteColor];
    [cell.dishImageView sd_setImageWithURL:[NSURL URLWithString:_secDataArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:_secDataArray[indexPath.row].head_img] placeholderImage:[UIImage imageNamed:@"userHeadImage"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.userNameLabel.text = _secDataArray[indexPath.row].nick;
    cell.userNameLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    cell.createTimeLabel.text = _secDataArray[indexPath.row].create_time;
    cell.createTimeLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    cell.agreeCountLabel.text = _secDataArray[indexPath.row].agree_count;
    cell.agreeCountLabel.font = [UIFont systemFontOfSize:13 * kWidthScale];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat itemWidth = (_selectedMoreCollectionView.bounds.size.width - CellMargin) / 2.0 ;
    CGFloat itemHeight = itemWidth / 32.0 * 42 ;
    return CGSizeMake(itemWidth, itemHeight);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *idString = self.secDataArray[indexPath.row].id_Data;
    FLLWorksDetailViewController *detailVC = [[FLLWorksDetailViewController alloc] init];
    detailVC.idString = idString;
    [self.navigationController pushViewController:detailVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
